<?php
/**
 * 公会好友导入
 * 
 * @author 	  jiaowei<jiaowei@178.com>
 * @version   $Id: guild.php 11841 2010-11-04 09:19:59Z jiaowei $
 */
class friends_model_guild extends Model
{
	const PAGE_SIZE = 30;

	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * 获取用户所属公会id
	 *
	 * @param  int $uid
	 * @return int
	 */
	public function getGuildFromUid($uid)
	{
		$gids = $this->db->fetchAll("SELECT `gid` FROM `app_guild_invite` WHERE `uid` = &uid", array('uid' => $uid));
		$result = array();
		foreach ($gids as $v) {
			$result[] = $v['gid'];
		}
		return $result;
	}
	
	/**
	 * 获取公会会员列表
	 *
	 * @param int $uid
	 * @param int $page
	 * @param int $pageSize
	 * @return array
	 */
	public function getUsersByPage($uid, $page, $pageSize)
	{
		$uid = (int) $uid;
		if (!$uid) return false;
		
		//好友用户
		$friends = friends_util_cache::getFriendIds($uid);
		$friends[] = $uid;//排除掉自己
		
		//已邀请用户
		$inviter = $this->getInviteds($uid);
		$inviter = array_merge($inviter, $friends);
		$inviter = array_flip($inviter);
		
		//同公会用户
		$gids = $this->getGuildFromUid($uid);
		$gids = (array) $gids;
		$gids = implode(',', $gids);
		if (!$gids) return false;
		$guildUsers = $this->db->fetchAll("SELECT `uid` FROM `app_guild_invite` WHERE `gid` IN ({$gids})");
		$users = array();
		foreach ($guildUsers as $v) {
			if (!array_key_exists($v['uid'], $inviter))
				$users[$v['uid']] = $v['uid'];
		}
		
		$offset    = ($page - 1) * $pageSize;
		$count 	   = count($users);
		$totalPage = ceil($count / $pageSize);
		$pageList  = array_slice($users, $offset, $pageSize);
		
		$pageList = friends_util_cache::getUserInfo($pageList);
		return array(
			'ROWS' => $pageList,
			'INFO' => array('COUNT' => $count, 'PAGE' => $page, 'PAGESIZE' => $pageSize, 'PAGECOUNT' => $totalPage)
		);
	}
	
	/**
	 * 添加已邀请记录
	 *
	 * @param int $uid
	 * @param array $inviter
	 * @return bool
	 */
	public function addInvitedLog($uid, $inviter)
	{
		if (!$inviter) return false;
		
		$str = '';
		foreach ((array)$inviter as $v)
			$str .= "('{$uid}', '{$v}', '{$_SERVER['REQUEST_TIME']}'),";
	
		$str = trim($str, ',');
		$sql = "INSERT ignore INTO `app_guild_invite_log` (`uid`, `invite_uid`, `ctime`) VALUES {$str}";
		return $this->db->query($sql);
	}
	
	/**
	 * 获取已邀请记录
	 *
	 * @param int $uid
	 * @return array
	 */
	public function getInviteds($uid)
	{
		$uid = (int) $uid;
		if (!$uid) return array();
		
		$inviters = $this->db->fetchAll("SELECT `invite_uid` FROM `app_guild_invite_log` WHERE `uid` = &uid", array('uid' => $uid));
		$result = array();
		foreach ((array)$inviters as $v)
			$result[] = $v['invite_uid'];
		return $result;
	}
	
	/**
	 * 获取所有公会成员id 暂无用
	 *
	 * @param unknown_type $uid
	 * @return unknown
	 */
	public function getListId($uid, $nums = 500)
	{
		$gid = $this->getGuildFromUid($uid);
		$listId = $this->db->fetchAll("SELECT `uid` FROM `app_guild_invite` WHERE `gid` = &gid AND `status`=0 LIMIT {$nums}", array('gid' => $gid));
		$result = array();
		foreach ($listId as $v) {
			$result[] = $v['uid'];
		}
		return $result;
	}
	
	/**
	 * 修改邀请状态 暂无用
	 *
	 * @param str $uids
	 * @return bool
	 */
	public function updateStatus($uid, $sender)
	{
		$gid = $this->getGuildFromUid($uid);
		
		$sender = implode(',', $sender);
		if (!$sender) return false;
		
		return $this->db->query("UPDATE `app_guild_invite` SET `status` = 1 WHERE `gid`='{$gid}' AND `uid` IN ({$sender})");
	}
	/**
	 * 获取用户名  暂无用
	 *
	 * @param str $uids
	 * @return array
	 */
	public function getUserName($sender)
	{
		$sender = implode(',', $sender);
		if (!$sender) return false;
		return $this->db->fetchAll("SELECT `uid`, `username` FROM `app_guild_invite` WHERE `uid` IN ($sender)");
	} 
	
	/**
	 * 添加好友关系
	 *
	 * @param int $hostId
	 * @param array $sender
	 * @return unknown
	 */
	public function addFriends($hostId, $sender)
	{
		if (!$sender) return false;
		
		$str = '';
		foreach ((array)$sender as $v) {
			$str .= "('{$v}', '{$hostId}'),";
		}
		$str = trim($str, ',');
		$sql = "INSERT ignore INTO `app_attention_request` (`uid`, `host_id`) VALUES {$str}";
		return $this->db->query($sql);
	}
	
	/**
	 * 好友关系检测 暂无用
	 *
	 * @param int $hostId
	 * @param str $uids
	 * @return array
	 */
	public function checkFriends($hostId, $sender)
	{
		$sender = implode(',', $sender);
		if (!$sender) return false;
		return $this->db->fetchAll("SELECT `uid` FROM `app_attentions` WHERE `host_id`=&hostId AND `uid` IN ($sender)", array('host_id' => $hostId));
	}
	
	/**
	 * 检测两个用户是否在同一个公会
	 *
	 * @param  int $hostId
	 * @param  int $uid
	 * @return int
	 */
	public function checkGuildUser($hostId, $uid)
	{
		$gid  = $this->getGuildFromUid($hostId);
		$gid2 = $this->getGuildFromUid($uid);
		return array_intersect($gid, $gid2) ? true : false;

		//return ($gid == $gid2) ? true : false;
	}
}
